import { message } from 'antd';
import moment from 'moment';
import {
  getListData,
  getRcrdListData,
  getAccuListData,
  getInsListData,
} from '@/services/analysises';

export default {
  state: {
    activeKey: '1',
    entryList: [],
    entryCount: 0,
    rcrdHandleCount: 0,
    accuHandleCount: 0,
    insHandleCount: 0,
    insHandelList: [],
    accuHandleList: [],
    rcrdHandleList: [],
    dateArr: [moment(), moment()],
    insDate: [moment(), moment()],
    accuDate: [moment(), moment()],
    rcrdDate: [moment(), moment()],
  },
  effects: {
    // 查询用户基本信息
    *fetchList({ payload }, { call, put, select }) {
      const activeKey = yield select(state => state.analysises.activeKey);
      const response = yield call(getListData, payload, activeKey);
      if (response.success) {
        yield put({
          type: 'getListData',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 获取社保列表
    *fetchInsList({ payload }, { call, put }) {
      const response = yield call(getInsListData, payload);
      if (response.success) {
        yield put({
          type: 'getInsListData',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 获取公积金列表
    *fetchAccuList({ payload }, { call, put }) {
      const response = yield call(getAccuListData, payload);
      if (response.success) {
        yield put({
          type: 'getAccuListData',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 获取档案列表
    *fetchRcrdList({ payload }, { call, put }) {
      const response = yield call(getRcrdListData, payload);
      if (response.success) {
        yield put({
          type: 'getRcrdListData',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
  },

  reducers: {
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 获取列表数据
    getListData(state, { payload }) {
      const salesData = [];
      for (let i = 0; i < (payload.list && payload.list.length); i += 1) {
        salesData.push({
          x: payload.list[i].areaName,
          y: payload.list[i].staNum,
        });
      }

      return {
        ...state,
        entryList: salesData,
        entryCount: payload.totalNum,
      };
    },
    // 获取档案数据
    getRcrdListData(state, { payload }) {
      const salesData = [];
      for (let i = 0; i < (payload.list && payload.list.length); i += 1) {
        salesData.push({
          x: payload.list[i].serName,
          y: payload.list[i].staNum,
        });
      }
      return {
        ...state,
        rcrdHandleList: salesData,
        rcrdHandleCount: payload.totalNum,
      };
    },
    // 获取公积金数据
    getAccuListData(state, { payload }) {
      const salesData = [];
      for (let i = 0; i < (payload.list && payload.list.length); i += 1) {
        salesData.push({
          x: payload.list[i].serName,
          y: payload.list[i].staNum,
        });
      }
      return {
        ...state,
        accuHandleList: salesData,
        accuHandleCount: payload.totalNum,
      };
    },
    // 获取社保数据
    getInsListData(state, { payload }) {
      const salesData = [];
      for (let i = 0; i < (payload.list && payload.list.length); i += 1) {
        salesData.push({
          x: payload.list[i].serName,
          y: payload.list[i].staNum,
        });
      }
      return {
        ...state,
        insHandelList: salesData,
        insHandleCount: payload.totalNum,
      };
    },
  },
};
